<script setup>
import { defineProps, ref } from 'vue'
import { ElImageViewer } from 'element-plus'

const props = defineProps({
  announcement: {
    type: Object,
    required: true,
    default: () => ({
      title: '',
      publishTime: '',
      author: '',
      authorAvatar: '',
      content: '',
      site: '',
      attachment: '',
      attachmentName: '',
      urlLink: ''
    })
  }
})
// 格式化时间的计算属性
const formatTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
// 默认头像URL
const defaultAvatar = '/default-avatar.png'

// 图片预览相关
const showImageViewer = ref(false)
const previewImage = ref('')

// 处理媒体点击
const handleMediaClick = (event) => {
  const target = event.target
  
  if (target.tagName === 'IMG') {
    previewImage.value = target.src
    showImageViewer.value = true
  } 
  else if (target.tagName === 'VIDEO') {
    if (document.fullscreenElement) {
      document.exitFullscreen()
    } else {
      target.requestFullscreen()
    }
  }
}

// 关闭图片预览
const closeImageViewer = () => {
  showImageViewer.value = false
}
</script>

<template>
  <div class="announcement-detail">
    <h1 class="title">{{ announcement.title }}</h1>
    
    <div class="meta-info">
      <div class="info-item">
        <span class="label">PublishTime:</span>
        <span class="value">{{ formatTime(announcement.publishTime) }}</span>
      </div>
      
      <div class="info-item author-info">
        <img 
          :src="announcement.user.head || defaultAvatar" 
          :alt="announcement.author"
          class="author-avatar"
        />
        <span class="label">Author:</span>
        <span class="value">{{ announcement.user.realname }}</span>
      </div>
    </div>
    
    <div class="content" v-html="announcement.content" @click="handleMediaClick"></div>
    
    <!-- 添加资源链接区域 -->
    <div class="attachment-section" v-if="announcement.urlLink">
      <span class="label">相关链接:</span>
      <div class="resource-item">
        <i class="el-icon-link"></i>
        <a 
          :href="announcement.urlLink" 
          target="_blank"
          class="resource-link"
          rel="noopener noreferrer"
        >
          {{ announcement.urlLink }}
        </a>
      </div>
    </div>

    <!-- 附件区域 -->
    <div class="attachment-section" v-if="announcement.attachment">
      <span class="label">附件下载:</span>
      <div class="attachment-item">
        <i class="el-icon-document"></i>
        <a 
          :href="announcement.attachment" 
          :download="announcement.attachmentName"
          class="attachment-link"
        >
          {{ announcement.attachmentName }}
        </a>
      </div>
    </div>

    <!-- 图片预览组件 -->
    <el-image-viewer
      v-if="showImageViewer"
      :url-list="[previewImage]"
      @close="closeImageViewer"
    />
  </div>
</template>

<style scoped>
.announcement-detail {
  max-width: 800px;
  margin: 2rem auto;
  padding: 2rem;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 1.8rem;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 1.5rem;
}

.meta-info {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.label {
  color: #666;
  font-size: 0.9rem;
}

.value {
  color: #333;
  font-weight: 500;
}

.content {
  line-height: 1.6;
  color: #444;
  font-size: 1.1rem;
  padding: 1rem;
  background-color: #f8f9fa;
  border-radius: 4px;
}

/* 添加富文本内容的样式 */
.content :deep(p) {
  margin-bottom: 1em;
}

.content :deep(img) {
  max-width: 100%;
  cursor: pointer;
  transition: opacity 0.3s;
}

.content :deep(img):hover {
  opacity: 0.9;
}

.content :deep(video) {
  max-width: 100%;
  cursor: pointer;
}

.content :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: 1em;
}

.content :deep(td), 
.content :deep(th) {
  border: 1px solid #ddd;
  padding: 8px;
}

.author-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 0.5rem;
}

.author-info {
  display: flex;
  align-items: center;
}

.resource-section {
  margin-top: 16px;
  padding: 8px 0;
  border-top: 1px solid #eee;
}

.resource-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.resource-link {
  color: #409EFF;
  text-decoration: none;
  cursor: pointer;
}

.resource-link:hover {
  color: #66b1ff;
  text-decoration: underline;
}

.attachment-section {
  margin-top: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.attachment-link {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
}

.attachment-link:hover {
  color: #66b1ff;
  text-decoration: underline;
}

.el-icon-document {
  font-size: 18px;
  color: #909399;
}
</style>